﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using CalemShop.Forms;

namespace CalemShop
{
    public partial class FrmReportIncome : FrmBaseDock
    {
        private int lastShowReportToday = 0;

        public FrmReportIncome()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }

        public FrmReportIncome(FrmCalemShop shop) : 
            base(shop)
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }

        private void btToday_Click(object sender, EventArgs e)
        {
            DoReportIncomeToday();
        }

        void DoReportIncomeToday()
        {
            lastShowReportToday = Environment.TickCount;
            StringBuilder builder = new StringBuilder();
            builder.Append("Ngày :" + DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss"));
            builder.Append(System.Environment.NewLine);
            builder.Append("--------------------------------------------------------");
            builder.Append(System.Environment.NewLine);
            builder.Append("+ Tổng tiền thu được :");
            builder.Append(Database.totalIncomeToday().ToString("#,0")).Append(" VNĐ");

            builder.Append(System.Environment.NewLine);
            builder.Append("+ Số khách trả hàng :");
            builder.Append(Database.countInvoiceReturnToday()).Append(" lần");

            builder.Append(System.Environment.NewLine);
            builder.Append("+ Tổng số hóa đơn bán :");
            string today = DateTime.Now.ToString("yyyy-MM-dd");
            builder.Append(Database.countInvoice(today, today));
            builder.Append(System.Environment.NewLine);
            builder.Append("--------------------------------------------------------");
            builder.Append(System.Environment.NewLine);
            DataTable db = Database.reportSellers(today, today);
            if (db != null)
            {
                foreach (DataRow r in db.Rows)
                {
                    string dname = (string)r["seller"];
                    double value = double.Parse(r["sellerIncome"].ToString());
                    builder.Append(string.Format("* {0} bán được : {1}", dname, value.ToString("#,0")));
                    builder.Append(System.Environment.NewLine);
                }

            }

            txtTodayReport.Text = builder.ToString();
            txtTodayReport.Refresh();
        }

        private void btXem_Click(object sender, EventArgs e)
        {
            StringBuilder builder = new StringBuilder();
            string from = dateTimePickerFrom.Value.ToString("yyyy-MM-dd");
            string to = dateTimePickerTo.Value.ToString("yyyy-MM-dd");
            builder.Append("Từ ngày :" + dateTimePickerFrom.Value.ToString("yyyy-MM-dd") + " tới ngày : " + dateTimePickerTo.Value.ToString("yyyy-MM-dd"));
            builder.Append(System.Environment.NewLine);
            builder.Append("--------------------------------------------------------");
            builder.Append(System.Environment.NewLine);
            builder.Append("+ Tổng tiền thu được :");
            builder.Append(Database.totalIncome(from, to).ToString("#,0")).Append(" VNĐ");

            builder.Append(System.Environment.NewLine);
            builder.Append("+ Số khách trả hàng :");
            builder.Append(Database.countInvoiceReturn(from, to)).Append(" lần");

            builder.Append(System.Environment.NewLine);
            builder.Append("+ Tổng số hóa đơn bán :");
            builder.Append(Database.countInvoice(dateTimePickerFrom.Value.ToString("yyyy-MM-dd"), dateTimePickerTo.Value.ToString("yyyy-MM-dd")));

            builder.Append(System.Environment.NewLine);
            builder.Append("--------------------------------------------------------");
            builder.Append(System.Environment.NewLine);
            DataTable db = Database.reportSellers(dateTimePickerFrom.Value.ToString("yyyy-MM-dd"), dateTimePickerTo.Value.ToString("yyyy-MM-dd"));
            if (db != null)
            {
                foreach (DataRow r in db.Rows)
                {
                    string dname = (string)r["seller"];
                    double value = double.Parse(r["sellerIncome"].ToString());
                    builder.Append(string.Format("* {0} bán được : {1}", dname, value.ToString("#,0")));
                    builder.Append(System.Environment.NewLine);
                }

            }

            txtView.Text = builder.ToString();
            doShowIncomeGraph();
        }

        void doShowIncomeGraph()
        {
            try
            {
                chart1.Series.Clear();
                Series serial = new Series("income");
                serial.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
                serial.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
                chart1.Series.Add(serial);
                chart1.Palette = ChartColorPalette.Excel;
                chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
                chart1.ChartAreas[0].AxisX.MinorGrid.Enabled = false;
                chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
                chart1.ChartAreas[0].AxisY.MinorGrid.Enabled = false;

                string frmDate = dateTimePickerFrom.Value.ToString("yyyy-MM-dd");
                string toDate = dateTimePickerTo.Value.ToString("yyyy-MM-dd");
                chart1.Titles.Clear();
                chart1.Titles.Add("Biểu đồ doanh thu");
                chart1.Titles[0].Font = new Font("Tahoma", 15);
                DataTable db = Database.reportIncome(frmDate, toDate);
                if (db != null)
                {
                    foreach (DataRow r in db.Rows)
                    {
                        DateTime dname = (DateTime)r["dateIncome"];
                        double value = double.Parse(r["income"].ToString());
                        serial.Points.AddXY(dname.ToString("dd-MM"), value);
                    }
                    chart1.Show();
                }
                else
                {
                    chart1.Hide();
                }
            }
            catch (Exception e)
            {
            }

        }

        void doShowSellerGraph()
        {
            try
            {
                chart1.Palette = ChartColorPalette.Pastel;
                chart1.Titles.Clear();
                chart1.Titles.Add("Biểu đồ năng suất bán hàng của nhân viên");
                chart1.Titles[0].Font = new Font("Tahoma", 15);
                chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
                chart1.ChartAreas[0].AxisX.MinorGrid.Enabled = false;
                chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
                chart1.ChartAreas[0].AxisY.MinorGrid.Enabled = false;

                string frmDate = dateTimePickerFrom.Value.ToString("yyyy-MM-dd");
                string toDate = dateTimePickerTo.Value.ToString("yyyy-MM-dd");
                chart1.Series.Clear();
                DataTable db = Database.reportSellers(frmDate, toDate);
                if (db != null)
                {
                    foreach (DataRow r in db.Rows)
                    {
                        string dname = (string)r["seller"];
                        Series serial = new Series(dname.ToLower());
                        chart1.Series.Add(serial);
                        serial.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
                        serial.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
                        //double value = double.Parse(r["selling"].ToString());
                        double value = double.Parse(r["sellerIncome"].ToString());
                        //serial.Points.AddXY(string.Format("{0}{2}({1})", dname, value.ToString("#,0"), System.Environment.NewLine), value);
                        serial.Points.Add(value);
                    }

                    chart1.Legends.Add("nhan vien");
                    chart1.Show();
                }
                else
                {
                    chart1.Hide();
                }
            }
            catch (Exception e)
            {
            }

        }

        private void btDoanhThu_Click(object sender, EventArgs e)
        {
            doShowIncomeGraph();
        }

        private void btNhanVien_Click(object sender, EventArgs e)
        {
            doShowSellerGraph();
        }

        private void tabMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabToday.Equals(tabMain.SelectedTab))
            {
                if (Environment.TickCount - lastShowReportToday > 10000)
                {
                    DoReportIncomeToday();
                }
            }
        }

        private void FrmReportIncome_Load(object sender, EventArgs e)
        {
            DoReportIncomeToday();
        }
    }
}
